#include<cstdio>//uncle-lu
#include<cstring>
#include<vector>
#include<algorithm>
template<class T>void read(T &x)
{
	x=0;int f=0;char ch=getchar();
	while(ch<'0'||ch>'9') { f|=(ch=='-'); ch=getchar(); }
	while(ch<='9'&&ch>='0') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
	x = f ? -x : x;
	return ;
}

std::vector<int>edge[1010];
int F[1010][300];
int cnt[1010];
int n, m;

void DFS(int x, int fa)
{
	cnt[x] = 1;
	for (int i = 0; i < (int)edge[x].size(); i++) 
	{
		int v = edge[x][i];
		if( v == fa )continue;
		DFS(v, x);
		cnt[x] += cnt[v];
		for (int j = std::min(m+1, cnt[x]); j >= 1; j--) 
			for (int k = 0; k <= std::min(cnt[v], j-1); k++) 
				F[x][j] = std::max(F[x][j], F[x][j-k] + F[v][k]);
	}

	return ;
}

int main()
{
	read(n);read(m);
	int temp;
	for (int i = 1; i <= n; i++) 
	{
		read(temp);
		read(F[i][1]);
		edge[temp].push_back(i);
		edge[i].push_back(temp);
	}

	DFS(0, 0);

	printf("%d", F[0][m+1]);
	return 0;
}
